package com.xk.demo.ptDuty.control;

import com.xk.demo.common.control.BaseController;
import com.xk.demo.mybatis.entity.PtDutyEntity;
import com.xk.demo.mybatis.entity.PtRoleEntity;
import com.xk.demo.mybatis.flexigrid.PadingRstType;
import com.xk.demo.mybatis.flexigrid.PagingBean;
import com.xk.demo.mybatis.ztree.ZtreeEntity;
import com.xk.demo.ptDuty.entity.PtDutySearchEntity;
import com.xk.demo.ptDuty.service.PtDutyService;
import com.xk.demo.role.entity.SearchRoleEntity;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.MessageSource;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author:XK
 * @Date: Created in 17:22 2022/5/16
 * @Description：
 **/
@Controller
@RequestMapping("/ptDuty")
public class PtDutyController extends BaseController {

    private Logger logger =  LogManager.getLogger(this.getClass().getName());
    @Resource
    private PtDutyService ptDutyService;

    @Secured("ROLE_gqgy_data_duty")

    @RequestMapping("/ptDutypage")
    public String dutyPage(){
        return "/duty/dutyPage";
    }

    @RequestMapping("/getDutyInfoListByPage")//分页
    @ResponseBody
    public PadingRstType<PtDutyEntity> getDutyInfoListByPage(PtDutySearchEntity search, PagingBean paging){
        logger.info(search);
        PadingRstType<PtDutyEntity> ptUserEntityPadingRstType = ptDutyService.getDutyInfoListByPage(search,paging);
        return ptUserEntityPadingRstType;
    }


    @RequestMapping("/getDutyZtreeList")
    @ResponseBody
    public List<ZtreeEntity> getDutyZtreeList(){
        List<ZtreeEntity> list=ptDutyService.getDutyZtreeList();
        return list;
    }

    @ResponseBody
    @RequestMapping("/listByOrgan")
    public List<PtDutySearchEntity> listByOrgan(String ORGAN_UUID){
        //参数是组织机构的uuid，需要通过name查出对应的ORGAN_UUID
        return ptDutyService.listByOrgan(ORGAN_UUID);
    }


    @ResponseBody
    @RequestMapping("/insertNewPt")
    public String insertNewPt(@RequestParam("name") String name,@RequestParam("organUuid")String ORGAN_UUID){
        PtDutyEntity ptDutyEntity = new PtDutyEntity();
        ptDutyEntity.setName(name);
        ptDutyEntity.setOrganUuid(ORGAN_UUID);
        if(ptDutyService.insertNewPt(ptDutyEntity)==1){
            return getSuccessMessag("duty.insert.success");
        }else {
            return getSuccessMessag("duty.insert.fail");
        }


    }

    @ResponseBody
    @RequestMapping("/deleteDutyById")
    public String deleteDuty(@RequestParam("dutyidlist") List<String> dutyidlist){
        for (String dutyid :
                dutyidlist) {
            ptDutyService.deleteDuty(dutyid);
        }
        return getSuccessMessag("duty.delete.success");
    }

}
